package com.main.rentsystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.main.rentsystem.mapper.HouseMapper;
import com.main.rentsystem.mapper.UserMapper;
import com.main.rentsystem.pojo.dto.OrderHouseDTO;
import com.main.rentsystem.pojo.entity.House;
import com.main.rentsystem.pojo.dto.HousePageQueryDTO;
import com.main.rentsystem.pojo.entity.OrderHouse;
import com.main.rentsystem.pojo.entity.PageBean;
import com.main.rentsystem.pojo.entity.User;
import com.main.rentsystem.pojo.vo.OrderHouseVO;
import com.main.rentsystem.service.HouseServer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static com.main.rentsystem.controller.user.UserController.USER_ID;
import static com.main.rentsystem.pojo.entity.OrderHouse.UNDER_REVIEW;

@Service
public class HouseServerimpl implements HouseServer {

    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 房屋分页查询
     * @param housePageQueryDTO
     * @return
     */
    public PageBean pageQuery(HousePageQueryDTO housePageQueryDTO) {
        PageHelper.startPage(housePageQueryDTO.getPage(),housePageQueryDTO.getPageSize());
        Page<House> page = houseMapper.pageQuery(housePageQueryDTO);
        return new PageBean(page.getTotal(),page.getResult());
    }

    /**
     * 预约看房
     * @param house
     */
    public void OrderHouse(House house) {
        OrderHouse orderHouse = new OrderHouse();
        BeanUtils.copyProperties(house,orderHouse);
        orderHouse.setUserId(USER_ID);
        orderHouse.setOrderTime(LocalDate.now());
        orderHouse.setStatus(UNDER_REVIEW);
        houseMapper.InsertHouse(orderHouse);
    }

    /**
     * 获取预约看房记录
     */
    public List<OrderHouseVO> GetOrderHouse(OrderHouseDTO orderHouseDTO) {
        List<OrderHouse> orderHouseList= houseMapper.GetHouse(orderHouseDTO);
        List<OrderHouseVO> orderHouseVOList=new ArrayList<>();
        for (OrderHouse orderHouse : orderHouseList) {
            OrderHouseVO orderHouseVO = new OrderHouseVO();
            BeanUtils.copyProperties(orderHouse,orderHouseVO);
            User user = userMapper.getUserById(orderHouse.getUserId());
            orderHouseVO.setUserName(user.getName());
            orderHouseVOList.add(orderHouseVO);
        }
        return orderHouseVOList;
    }

    /**
     * 新增房屋
     * @param house
     */
    public void saveHouse(House house) {
        houseMapper.insertHouse(house);
    }

    /**
     * 修改房屋
     * @param house
     */
    public void updateHouse(House house) {
        houseMapper.updateHouse(house);
    }

    /**
     * 批量删除房屋
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        houseMapper.deleteBatch(ids);
    }

    /**
     * 修改预约状态
     * @param orderHouseDTO
     */
    public void updateOrderHouse(OrderHouseDTO orderHouseDTO) {
        houseMapper.updateOrderHouse(orderHouseDTO);
    }

}
